﻿namespace EFS.RC.CCC.Deployment.Tables
{
	public static class Teams
	{
		/// Returns an object from a data row
		public static EFS.RC.CCC.Deployment.Entities.Team FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CCC.Deployment.Entities.Team dataObj = new EFS.RC.CCC.Deployment.Entities.Team();
			
			// ID
			dataObj.ID = EFS.DB.Common.Functions.Value.Int32(row, "ID");

			// SIP
			dataObj.Name = EFS.DB.Common.Functions.Value.String(row, "NAME");

			// ENDPOINT_ID
			dataObj.EndPointID = EFS.DB.Common.Functions.Value.Int32(row, "ENDPOINT_ID");

			// PRIORIY
			dataObj.Priority = EFS.DB.Common.Functions.Value.Int32(row, "PRIORIY");

			// ok
			return dataObj;
		}

		/// GetAll
		public static EFS.RC.CCC.Deployment.Entities.Team[] GetByEndPointID(
			System.Int32 endPointID)
		{
			// get data
			System.Data.DataSet resultDataSet =
				EFS.RC.CCC.Deployment.Connection.Instance.DoSelect(
					"[dbo].[Teams_GetByEndPointID]",
					new System.Data.SqlClient.SqlParameter[] 
					{
						new System.Data.SqlClient.SqlParameter("@endpoint_id", endPointID)
					});

			// create list
			System.Collections.Generic.List<EFS.RC.CCC.Deployment.Entities.Team> list = new System.Collections.Generic.List<EFS.RC.CCC.Deployment.Entities.Team>();

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					list.Add(FromTableRow(row));
				}
			}

			// ok
			return list.ToArray();
		}

		/// GetByID
		public static EFS.RC.CCC.Deployment.Entities.Team GetByID(System.Int32 id)
		{
			// get data
			System.Data.DataSet resultDataSet =
				EFS.RC.CCC.Deployment.Connection.Instance.DoSelect(
					"[dbo].[Teams_GetByID]",
					new System.Data.SqlClient.SqlParameter[] 
					{
						new System.Data.SqlClient.SqlParameter("@id", id)
					});

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					return FromTableRow(row);
				}
			}

			// none found
			return null;
		}

		/// GetByNameAndEndPointID
		public static EFS.RC.CCC.Deployment.Entities.Team GetByNameAndEndPointID(System.String teamName, System.Int32 endpointID)
		{
			// get data
			System.Data.DataSet resultDataSet =
				EFS.RC.CCC.Deployment.Connection.Instance.DoSelect(
					"[dbo].[Teams_GetByNameAndEndPointID]",
					new System.Data.SqlClient.SqlParameter[] 
					{
						new System.Data.SqlClient.SqlParameter("@name", teamName),
						new System.Data.SqlClient.SqlParameter("@endpoint_id", endpointID)
					});

			// load data
			if (!resultDataSet.HasErrors && resultDataSet.Tables.Count > 0)
			{
				System.Data.DataTable table = resultDataSet.Tables[0];
				foreach (System.Data.DataRow row in table.Rows)
				{
					return FromTableRow(row);
				}
			}

			// none found
			return null;
		}

		/// Create
		public static System.Int32? Create(
			System.String name,
			System.Int32 endPointID,
			System.Int32 priority,
			System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			// ok
			return
				EFS.RC.CCC.Deployment.Connection.Instance.Create(
					"[dbo].[Teams_Create]",
					new System.Data.SqlClient.SqlParameter[]
				{
					new System.Data.SqlClient.SqlParameter("@name", name),
					new System.Data.SqlClient.SqlParameter("@endpoint_id", endPointID),
					new System.Data.SqlClient.SqlParameter("@priority", priority)
				}, sqlTransaction);
		}

		/// Update
		public static void Update(
			System.Int32 id,
			System.String name,
			System.Int32 endPointID,
			System.Int32 priority)
		{
			EFS.RC.CCC.Deployment.Connection.Instance.Update(
				"[dbo].[Teams_Update]",
				new System.Data.SqlClient.SqlParameter[]
				{
					new System.Data.SqlClient.SqlParameter("@id", id),
					new System.Data.SqlClient.SqlParameter("@name", name),
					new System.Data.SqlClient.SqlParameter("@endpoint_id", endPointID),
					new System.Data.SqlClient.SqlParameter("@priority", priority)
				});
		}

		/// Delete
		public static void Delete(System.Int32 id)
		{
			EFS.RC.CCC.Deployment.Connection.Instance.Delete("[dbo].[Teams_Delete]", id);
		}
	}

}